package org.hydrakyoufeng.stuplatform.config;

import org.hydrakyoufeng.stuplatform.interceptor.IpLimitInterceptor;
import org.hydrakyoufeng.stuplatform.interceptor.PermissionInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * 拦截器配置
 */
@Configuration
public class InterceptorConfig implements WebMvcConfigurer {

    private final PermissionInterceptor permissionInterceptor;

    private final IpLimitInterceptor ipLimitInterceptor;

    @Autowired
    public InterceptorConfig(PermissionInterceptor permissionInterceptor,
                             IpLimitInterceptor ipLimitInterceptor) {
        this.permissionInterceptor = permissionInterceptor;
        this.ipLimitInterceptor = ipLimitInterceptor;
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(permissionInterceptor)
                // 匹配路径
                .addPathPatterns("/**")
                // 排除路径
                .excludePathPatterns("/login/**","/swagger-ui/*","/swagger-ui.html")
                .order(2);

        registry.addInterceptor(ipLimitInterceptor).addPathPatterns("/**").order(1);
    }
}
